package com.topnav.mc.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.js.lib.result.Result;
import com.topnav.mc.admin.bean.OrgTreeSelectBean;
import com.topnav.mc.admin.entity.bean.OrganizationBean;
import com.topnav.mc.admin.entity.param.OrganizationParam;
import com.topnav.mc.admin.entity.wrapper.OrganizationWrapper;
import com.topnav.mc.admin.security.PermissionGrade;
import com.topnav.mc.base.mc.McController;
import com.topnav.mc.exception.ExceptionMsg;
import com.topnav.mc.uitls.PermissionGradeUtil;
import com.topnav.mc.uitls.QueryPage;
import com.topnav.mc.uitls.ResultErrorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.topnav.mc.admin.entity.Organization;
import com.topnav.mc.admin.service.IOrganizationService;
import io.swagger.annotations.Api;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 说明：组织机构控制 层接口
 * @类名: OrganizationCurdController
 * <p>
 *
 * </p>
 * @author   kenny
 * @Date	 2021年11月19日上午10:56:34
 */
@Api(value = "admin/organization", tags = "组织机构管理")
@RestController
@CrossOrigin
@RequestMapping("admin/organization")
//public class OrganizationController extends CurdController<Organization,IOrganizationService>{
public class OrganizationController extends McController<Organization, OrganizationBean,OrganizationParam,
        IOrganizationService, OrganizationWrapper> {

    @Autowired
    private IOrganizationService organizationService;
    @Resource
    private PermissionGradeUtil pmGradeUtil;

    /**
     * 分页查询,只查第一层
     * @param p
     * @return
     */
    //@ApiOperation(value = "带有条件的分页查询")
    //@PostMapping("/query/toplayer/page")
    @ResponseBody
    public Result queryToplayerPage(@Validated(IPage.class) @RequestBody QueryPage<OrganizationParam> p,OrganizationWrapper wrapper) {
        PermissionGrade permissionGrade = pmGradeUtil.getPmGrade();
        Page datas = organizationService.pageInfo(p.getPage(), wrapper);


        return Result.ok(datas);
    }

    /**
     * 只查询下一级
     * @param parentId
     * @return
     */
    //@GetMapping("/query/childrens")
    @ResponseBody
    public Result queryListByparent(Long parentId) {
        QueryWrapper<Organization> wrapper = new QueryWrapper<Organization>().eq("parent_id",parentId);;
        List<Organization> orgs = organizationService.list(wrapper);
        return Result.ok(orgs);
    }

    /**
     * 深度查询所有机构, 并组成树结构的数据
     * @return
     */
    //@ApiOperation(value = "深度查询所有机构")
    @GetMapping("/query/depthChildrens")
    @ResponseBody
    public Result queryDepthChildrens(){
        QueryWrapper<Organization> wrapper = new QueryWrapper<Organization>();
        PermissionGrade permissionGrade = pmGradeUtil.getPmGrade();
        if (permissionGrade != null){
            if(permissionGrade.getPermissionLevel() == 1){
                wrapper.eq("id",permissionGrade.getOrgId());
            }else if(permissionGrade.getPermissionLevel() == 2){
                wrapper.eq("id",permissionGrade.getOrgId());
            }else if(permissionGrade.getPermissionLevel() == 3 || permissionGrade.getPermissionLevel() == 4){
                wrapper.like("path",permissionGrade.getOrgPath());
            }else if(permissionGrade.getPermissionLevel() > 4){
                /** 系统管理员则显示第一级 */
                wrapper.isNull("parent_id");
            }
        } else{
            ResultErrorUtil.byExceptionMsg(ExceptionMsg.ERROR_AUTHENTICATION_EFFE);
        }
        wrapper.orderByAsc("path");

        List<Organization> list = organizationService.list(wrapper);
        return Result.ok(changeToTree(list));
    }

    /**
     * 换成树形结构
     * @param list
     * @return
     */
    private List<OrgTreeSelectBean> changeToTree(List<Organization> list){
        List<OrgTreeSelectBean> result = null;//new ArrayList<OrgTreeSelect>();
        if (!CollectionUtils.isEmpty(list)){
            result = new ArrayList<OrgTreeSelectBean>();
            for(Organization o : list){
                OrgTreeSelectBean orgTreeSelectBean = new OrgTreeSelectBean()
                        .setId(o.getId().toString())
                        .setLabel(o.getShortName())
                        .setPath(o.getPath());
                orgTreeSelectBean.getArrt().put("code",o.getCode());
                orgTreeSelectBean.getArrt().put("name",o.getName());

                QueryWrapper<Organization> wrapper = new QueryWrapper<Organization>().eq("parent_id",o.getId());
                List<Organization> orgs = organizationService.list(wrapper);
                for(Organization o1:orgs){
                    orgTreeSelectBean.setChildren(changeToTree(orgs));
                }
                result.add(orgTreeSelectBean);
            }

        }
        return result;


    }
}
